package com.oscchina.xushao.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.oscchina.xushao.R;
import com.oscchina.xushao.adapter.ListViewNewsAdapter;
import com.oscchina.xushao.app.AppContext;
import com.oscchina.xushao.app.AppException;
import com.oscchina.xushao.bean.News;
import com.oscchina.xushao.bean.NewsList;
import com.oscchina.xushao.common.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 资讯-资讯的子类，包括最新资讯
 *
 * @author XuShaoJie
 * @version 2013-07-05
 */
public class InfoNewsFragment extends Fragment {

    //   private PullToRefreshListView mPullRefreshListView;
    private ListView mPullRefreshListView;
    private ListViewNewsAdapter mNewsAdapter;
    private List<News> lvNewsDataList = new ArrayList<News>();
    private int lvSumData;// 加载的总数目

    private AppContext appContext;// 全局Context

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appContext = (AppContext) getActivity().getApplication();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_ptr_list, container, false);
        mPullRefreshListView = (ListView) view.findViewById(R.id.pull_refresh_list);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = null;
                // 判断是否是TextView
                if (view instanceof TextView) {
                    news = (News) view.getTag();
                } else {
                    TextView tv = (TextView) view.findViewById(R.id.news_listitem_title);
                    news = (News) tv.getTag();
                }
                if (news == null) {
                    return;
                }
                // 跳转到新闻详情
                UIHelper.showNewsRedirect(view.getContext(), news);
            }
        });

        mNewsAdapter = new ListViewNewsAdapter(getActivity(), lvNewsDataList, R.layout.news_listitem);
        mPullRefreshListView.setAdapter(mNewsAdapter);

        // TODO 第一次启动时候加载数据
        new GetFansTask().execute();

    }

    /**
     * 加载数据AsyncTask
     */
    private class GetFansTask extends AsyncTask<Void, Void, NewsList> {

        @Override
        protected NewsList doInBackground(Void... params) {
            // 当前pageIndex
            int pageIndex = lvSumData / 20;
            try {
                return appContext.getNewsList(NewsList.CATALOG_ALL, pageIndex, true);
            } catch (AppException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(NewsList result) {
            super.onPostExecute(result);

            if (result != null) {
                List<News> newsList = result.getNewslist();
                // 加载成功且如果有数据
                if (newsList != null) {
                    lvSumData += newsList.size();
                    lvNewsDataList.addAll(newsList);
                    mNewsAdapter.notifyDataSetChanged();
                }
                // 加载失败或者没有数据
                else {
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(), "加载异常", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
